package com.somlsis.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.base.AppConfig
import com.base.BaseAdapter
import com.clj.fastble.data.BleDevice
import com.somlsis.R
import com.somlsis.bean.Device

class DeviceAdapter : BaseAdapter<Device, DeviceAdapter.ViewHolder> {
    private var listener: OnDeviceClickListener

    constructor(
        context: Context,
        onDeviceClickListener: OnDeviceClickListener
    ) : super(context) {
        listener = onDeviceClickListener
    }

    class ViewHolder : RecyclerView.ViewHolder {
        var viewType: Int = AppConfig.TYPE_DEVICE
        var name: TextView? = null
        var mac: TextView? = null
        var connectStateIV: ImageView? = null
        var connectState: TextView? = null
        var addDevice: LinearLayout? = null

        constructor(itemView: View, type: Int) : super(itemView) {
            viewType = type
            if (viewType == AppConfig.TYPE_DEVICE) {
                name = itemView.findViewById(R.id.device_name)
                mac = itemView.findViewById(R.id.device_mac)
                connectStateIV = itemView.findViewById(R.id.connect_state_iv)
                connectState = itemView.findViewById(R.id.connect_state_text)
            } else {
                addDevice = itemView.findViewById(R.id.add_device)
            }
        }
    }

    override fun getItemCount(): Int {
        return dataList.size + 1
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {

        return if (viewType == AppConfig.TYPE_DEVICE) {
            val inflate: View =
                LayoutInflater.from(mContext).inflate(R.layout.item_device, parent, false)
            ViewHolder(inflate, AppConfig.TYPE_DEVICE)
        } else {
            val inflate: View =
                LayoutInflater.from(mContext).inflate(R.layout.layout_add_device, parent, false)
            ViewHolder(inflate, AppConfig.TYPE_ADD_DEVICE)
        }
    }

    override fun onBindView(holder: ViewHolder, position: Int, data: Device?) {

        if (holder.viewType == AppConfig.TYPE_DEVICE) {
            data?.let {
                val deviceInfo = data.deviceInfo
                holder.name?.let { v -> v.text = deviceInfo.serial_name }
                holder.mac?.let { v -> v.text = deviceInfo.serial_mac }

                holder.itemView.setOnClickListener { _ ->
                    listener.onItemClick(position, it)
                }
                if (it.isConnect){
                    holder.connectStateIV?.let{v->
                        v.setImageResource(R.mipmap.icon_connected)
                    }
                    holder.connectState?.let{v->
                        v.text=mContext.getText(R.string.connected)
                    }
                }else{
                    holder.connectStateIV?.let{v->
                        v.setImageResource(R.mipmap.icon_ununited)
                    }
                    holder.connectState?.let{v->
                        v.text=mContext.getText(R.string.ununited)
                    }
                }
            }
        } else {
            holder.itemView.setOnClickListener {
                listener.onAddViewClick()
            }
        }

    }

    override fun getItemViewType(position: Int): Int {
        if (position == dataList.size) {
            return AppConfig.TYPE_ADD_DEVICE
        } else {
            return AppConfig.TYPE_DEVICE
        }
    }

    interface OnDeviceClickListener {
        fun onItemClick(position: Int, data: Device?)
        fun onAddViewClick()
    }
}